<html>
<head>
  <title>Evernote Export</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="YXBJ Windows/600753 (zh-CN, DDL); Windows/10.0.0 (Win64);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="1330"/>

<div>
<span><div><div>inRange()函数</div><div><br/></div><div>      OpenCV中的inRange()函数可实现二值化功能（这点类似threshold()函数），更关键的是可以同时针对多通道进行操作，使用起来非常方便！主要是将在两个阈值内的像素值设置为白色（255），而不在阈值区间内的像素值设置为黑色（0），该功能类似于之间所讲的双阈值化操作。</div><div><br/></div><div>函数原型（C++）：</div><div><br/></div><div>    void inRange(InputArray src, InputArray lowerb,</div><div>                              InputArray upperb, OutputArray dst);</div><div>官方文档中的解释：Checks if array elements lie between the elements of two other arrays.即检查数组元素是否在另外两个数组元素值之间。这里的数组通常也就是矩阵Mat或向量。请注意：该函数输出的dst是一幅二值化之后的图像。</div><div><br/></div><div>参数解释：</div><div><br/></div><div>参数1：输入要处理的图像，可以为单通道或多通道。</div><div>参数2：包含下边界的数组或标量。</div><div>参数3：包含上边界数组或标量。</div><div>参数4：输出图像，与输入图像src 尺寸相同且为CV_8U 类型。</div><div><br/></div><div><span style="min-height: 10pt; font-size: 9.5pt; font-family: NSimSun;">rectangle() 图片上绘制矩阵</span></div><div><span style="font-size: 9pt; font-family: NSimSun;">img 图像. pt1 矩形的一个顶点。 pt2 矩形对角线上的另一个顶点 color 线条颜色 (RGB) 或亮度（灰度图像 ）(grayscale image）。 thickness 组成矩形的线条的粗细程度。取负值时（如 CV_FILLED）函数绘制填充了色彩的矩形。 line_type 线条的类型。见cvLine的描述 shift 坐标点的小数点位数。</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; overflow-x: auto; overflow-wrap: break-word; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="box-sizing: border-box; outline: 0px; font-size: 18px; overflow-x: auto; overflow-wrap: break-word; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(255, 0, 0); font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">如果需要在Mat类型的图上绘制矩形，选择cv::trctangle()</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; overflow-x: auto; overflow-wrap: break-word; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="background-color: rgb(255, 255, 255); font-size: 16px;"><span style="box-sizing: border-box; outline: 0px; font-size: 18px; overflow-x: auto; overflow-wrap: break-word; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(255, 0, 0); font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px; background-color: rgb(255, 255, 255);">在IplImage*类型的图上绘制矩形，选择cvRectangle() </span></span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; overflow-x: auto; overflow-wrap: break-word; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="min-height: 10pt; font-size: 9.5pt; color: rgb(77, 77, 77); font-family: NSimSun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">findContours()</span></div><div style="text-align: start;"><span style="font-size: 12pt; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;;">void findContours//提取轮廓，用于提取图像的轮廓</span></div><div style="text-align: start;"><span style="font-size: 12pt; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;;">(</span></div><div style="text-align: start;"><span style="font-size: 12pt; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;;">InputOutputArray image,//输入图像，必须是8位单通道图像，并且应该转化成二值的</span></div><div style="text-align: start;"><span style="font-size: 12pt; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;;">OutputArrayOfArrays contours,//检测到的轮廓，每个轮廓被表示成一个point向量</span></div><div style="text-align: start;"><span style="font-size: 12pt; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;;">OutputArray hierarchy,//可选的输出向量，包含图像的拓扑信息。其中元素的个数和检测到的轮廓的数量相等</span></div><div style="text-align: start;"><span style="font-size: 12pt; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;;">int mode,//说明需要的轮廓类型和希望的返回值方式</span></div><div style="text-align: start;"><span style="font-size: 12pt; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;;">int method,//轮廓近似方法</span></div><div style="text-align: start;"><span style="font-size: 12pt; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;;">Point offset = Point()</span></div><div style="text-align: start;"><span style="font-size: 12pt; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;;">)</span></div><div style="text-align: start;"><br/></div><div style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; overflow-x: auto; overflow-wrap: break-word; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="box-sizing: border-box; outline: 0px; font-size: 16px; overflow-x: auto; overflow-wrap: break-word; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif; font-variant-caps: normal; font-variant-ligatures: common-ligatures; line-height: 26px;">第一个参数是寻找轮廓的图像；</span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; overflow-x: auto; overflow-wrap: break-word; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="background-color: initial; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif; font-size: 16px; font-variant-caps: normal; font-variant-ligatures: common-ligatures; line-height: 26px;">第二个参数表示轮廓的检索模式，有四种（本文介绍的都是新的cv2接口）：</span></div><div style="text-align: start;">    cv2.RETR_EXTERNAL表示只检测外轮廓</div><div style="text-align: start;">    cv2.RETR_LIST检测的轮廓不建立等级关系</div><div style="text-align: start;">    cv2.RETR_CCOMP建立两个等级的轮廓，上面的一层为外边界，里面的一层为内孔的边界信息。如果内孔内还有一个连通物体，这个物体的边界也在顶层。</div><div style="margin-top: 1em; margin-bottom: 1em; text-align: start;"><span style="-en-paragraph:true;">    cv2.RETR_TREE建立一个等级树结构的轮廓。</span></div><p style="text-align: start;"></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: &quot;Microsoft YaHei&quot;, &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, sans-serif; font-size: 16px; color: rgb(77, 77, 77); line-height: 26px; overflow-x: auto; overflow-wrap: break-word; font-variant-ligatures: common-ligatures; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"></p><div style="text-align: start;">第三个参数method为轮廓的近似办法</div><div style="text-align: start;">    **cv2.CHAIN_APPROX_NONE存储所有的轮廓点，相邻的两个点的像素位置差不超过1**，即max（abs（x1-x2），abs（y2-y1））==1</div><div style="text-align: start;">    cv2.CHAIN_APPROX_SIMPLE压缩水平方向，垂直方向，对角线方向的元素，只保留该方向的终点坐标，例如一个矩形轮廓只需4个点来保存轮廓信息</div><div style="margin-top: 1em; margin-bottom: 1em; text-align: start;"><span style="-en-paragraph:true;">    cv2.CHAIN_APPROX_TC89_L1，CV_CHAIN_APPROX_TC89_KCOS使用teh-Chinl chain 近似算法</span></div><div style="text-align: start;"><br/></div><div style="text-align: start;"><span style="min-height: 10pt; font-size: 9.5pt; color: rgb(0, 0, 255); font-family: NSimSun;">static_cast</span></div><div style="text-align: start;"><br/></div><div style="text-align: start;"><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div>int i;</div><div>float f = 166.71;</div><div>i = static_cast&lt;int&gt;(f);</div><div><span style="font-size: 9pt; color: rgb(51, 51, 51); font-family: Monaco;">值为166</span></div></div><div><br/></div></div><div style="text-align: start;">一、static_cast和dynamic_cast使用方式</div><div style="text-align: start;">static_cast&lt; new_type &gt;(expression)</div><div style="text-align: start;">dynamic_cast&lt; new_type &gt;(expression)</div><div style="text-align: start;">备注：new_type为目标数据类型，expression为原始数据类型变量或者表达式。</div><div style="text-align: start;"><br/></div><div style="text-align: start;">二、static_cast详解：</div><div style="text-align: start;">static_cast相当于传统的C语言里的强制转换，该运算符把expression转换为new_type类型，用来强迫隐式转换如non-const对象转为const对象，编译时检查，用于非多态的转换，可以转换指针及其他，但没有运行时类型检查来保证转换的安全性。它主要有如下几种用法：</div><div style="text-align: start;"><br/></div><div style="text-align: start;">①用于类层次结构中基类（父类）和派生类（子类）之间指针或引用的转换。</div><div style="text-align: start;">进行上行转换（把派生类的指针或引用转换成基类表示）是安全的；</div><div style="text-align: start;">进行下行转换（把基类指针或引用转换成派生类表示）时，由于没有动态类型检查，所以是不安全的。</div><div style="text-align: start;">②用于基本数据类型之间的转换，如把int转换成char，把int转换成enum。</div><div style="text-align: start;">③把空指针转换成目标类型的空指针。</div><div style="text-align: start;">④把任何类型的表达式转换成void类型。</div><div style="text-align: start;">注意：static_cast不能转换掉expression的const、volatile、或者__unaligned属性</div><div style="text-align: start;"><br/></div><div style="text-align: start;">基本类型数据转换举例如下：</div><div style="text-align: start;"><br/></div><div style="text-align: start;">char a = 'a';</div><div style="text-align: start;">int b = static_cast&lt;char&gt;(a);//正确，将char型数据转换成int型数据</div><div style="text-align: start;"><br/></div><div style="text-align: start;">double *c = new double;</div><div style="text-align: start;">void *d = static_cast&lt;void*&gt;(c);//正确，将double指针转换成void指针</div><div style="text-align: start;"><br/></div><div style="text-align: start;">int e = 10;</div><div style="text-align: start;">const int f = static_cast&lt;const int&gt;(e);//正确，将int型数据转换成const int型数据</div><div style="text-align: start;"><br/></div><div style="text-align: start;">const int g = 20;</div><div style="text-align: start;">int *h = static_cast&lt;int*&gt;(&amp;g);//编译错误，static_cast不能转换掉g的const属性</div><div style="text-align: start;"><br/></div><div style="text-align: start;"><span style="min-height: 10pt; font-size: 9.5pt; font-family: NSimSun;">contourArea </span><span style="font-size: 30px; color: rgb(64, 64, 64); font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-weight: bold;">轮廓面积检测</span></div><div style="text-align: start;"><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">Parameters:</span></div><div style="text-align: start;"><span style="box-sizing: border-box; font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;">contour</span><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;"> </span><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">– Input vector of 2D points (contour vertices), stored in std::vector or Mat.</span></div><div style="text-align: start;"><span style="box-sizing: border-box; font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;">oriented</span><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;"> </span><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">– Oriented area flag. If it is true, the function returns a signed area value, depending on the contour orientation (clockwise or counter-clockwise). Using this feature you can determine orientation of a contour by taking the sign of an area. By default, the parameter is false, which means that the absolute value is returned.会有方向的话，就会返回有正负，否则就是绝对值。</span></div><div style="text-align: start;"><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(64, 64, 64); font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">例子</span></div><div style="text-align: start;"><br/></div><div style="text-align: start;"><span style="min-height: 10pt; font-size: 9.5pt; font-family: NSimSun;">boundingRect</span></div><div style="text-align: start;"><br/></div><div style="text-align: start;"><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(64, 64, 64); font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">boundingRect函数是用来计算轮廓的最小外接矩形，通常与findContours函数组合使用，findContours函数用来查找图像的轮廓，boundingRect获取轮廓的最小外接矩形！</span></div><div style="text-align: start;"><br/></div><div style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; word-break: break-word; font-size: 16px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="box-sizing: border-box; word-break: break-word; font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">(1) 第一个参数，InputArray array，一般为findContours函数查找的轮廓，包含轮廓的点集或者Mat；</span></div><div style="text-align: start;"><span style="background-color: rgb(255, 255, 255); font-size: 16px;"><span style="box-sizing: border-box; word-break: break-word; font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(64, 64, 64); font-family: -apple-system, BlinkMacSystemFont, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">(2) 返回值，Rect，返回值为最小外接矩形的Rect，即左上点与矩形的宽度和高度；</span></span></div><div style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; overflow-x: auto; overflow-wrap: break-word; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div>#include &lt;opencv2/opencv.hpp&gt;</div><div>#include &lt;iostream&gt;</div><div>using namespace std;</div><div>using namespace cv;</div><div>Rect roi;</div><div>void processFrame(Mat &amp;binary, Rect &amp;rect);</div><div>int main(int argc, char* argv) {</div><div>        // load video</div><div>        VideoCapture capture;</div><div>        capture.open(&quot;D:/vcprojects/images/video_006.mp4&quot;);</div><div>        if (!capture.isOpened()) {</div><div>               printf(&quot;could not find video file&quot;);</div><div>               return -1;</div><div>        }</div><div>        Mat frame, mask;</div><div>        Mat kernel1 = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));</div><div>        Mat kernel2 = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));</div><div>        namedWindow(&quot;input video&quot;, CV_WINDOW_AUTOSIZE);</div><div>        namedWindow(&quot;track mask&quot;, CV_WINDOW_AUTOSIZE);</div><div>        while (capture.read(frame)) {</div><div>               inRange(frame, Scalar(0, 127, 0), Scalar(120, 255, 120), mask); // 过滤</div><div>               morphologyEx(mask, mask, MORPH_OPEN, kernel1, Point(-1, -1), 1); // 开操作</div><div>               dilate(mask, mask, kernel2, Point(-1, -1), 4);// 膨胀</div><div>               imshow(&quot;track mask&quot;, mask);</div><div>               processFrame(mask, roi); // 轮廓发现与位置标定</div><div>               rectangle(frame, roi, Scalar(0, 0, 255), 3, 8, 0);</div><div>               imshow(&quot;input video&quot;, frame);</div><div>               // trigger exit</div><div>               char c = waitKey(100);</div><div>               if (c == 27) {</div><div>                       break;</div><div>               }</div><div>        }</div><div>        capture.release();</div><div>        waitKey(0);</div><div>        return 0;</div><div>}</div><div>void processFrame(Mat &amp;binary, Rect &amp;rect) {</div><div>        vector&lt;vector&lt;Point&gt;&gt; contours;</div><div>        vector&lt;Vec4i&gt; hireachy;</div><div>        findContours(binary, contours, hireachy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE,  Point(0, 0));</div><div>        if (contours.size() &gt; 0) {</div><div>               double maxArea = 0.0;</div><div>               for (size_t t = 0; t &lt; contours.size(); t++) {</div><div>                       double area = contourArea(contours[static_cast&lt;int&gt;(t)]);</div><div>                       if (area &gt; maxArea) {</div><div>                              maxArea = area;</div><div>                              rect = boundingRect(contours[static_cast&lt;int&gt;(t)]);</div><div>                       }</div><div>               }</div><div>        }</div><div>        else {</div><div>               rect.x = rect.y = rect.width = rect.height = 0;</div><div>        }</div><div>}</div></div><div style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; overflow-x: auto; overflow-wrap: break-word; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></div><div style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; overflow-x: auto; overflow-wrap: break-word; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></div><div style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-size: 16px; overflow-x: auto; overflow-wrap: break-word; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></div></div><div><br/></div></span>
</div></body></html> 